package com.xuanyun.rn.badge;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import javax.annotation.Nullable;

public class NotificationBroadcastReceiver extends BroadcastReceiver {

    public static final String TAG = "NBroadcastReceiver";

    public static final String TYPE = "type";

    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        int type = intent.getIntExtra(TYPE, -1);

        if (type != -1) {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.cancel(type);
        }

        //TODO 处理点击事件

        if (action.equals("notification_clicked")) {
            Log.d(TAG, "notification  Click");

            WritableMap event = Arguments.createMap();
            androidToRNEvent((ReactContext) context, "", event);
        }

        //TODO 处理滑动清除和点击删除事件
        if (action.equals("notification_cancelled")) {
            Log.d(TAG, "notification  cancelled");
        }
    }

    public static void androidToRNEvent(ReactContext reactContext, String eventName, @Nullable WritableMap paramss) {
        reactContext
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, paramss);
    }
}
